package com.guigu.thirdpartyservices.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.gulicommon.entity.vo.UserInformationByGiteeVo;
import com.guigu.gulicommon.tools.HttpTool;
import com.guigu.thirdpartyservices.constant.request.gitee.GiteeGetRequestPathAndParamsConstant;
import com.guigu.thirdpartyservices.etity.vo.GiteeTokenVo;
import com.guigu.thirdpartyservices.propertes.GiteeProperty;
import com.guigu.thirdpartyservices.service.ThirdPartLoginService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedHashMap;

import static com.guigu.gulicommon.constant.enumes.res.SelectResCode.GET_GITEE_USER_INFO_SUCCESS;
import static com.guigu.thirdpartyservices.constant.request.gitee.GiteeGetRequestPathAndParamsConstant.GET_ACCESS_TOKEN;

@Service
@Log4j2
public class ThirdPartLoginServiceImpl implements ThirdPartLoginService {
    @Autowired
    private GiteeProperty giteeProperty;

    /**
     * 通过Gitee获取用户信息
     * <p>
     * 通过Gitee三方登陆获取获取到的code解析用户信息
     *
     * @param code Gitee返回的code
     * @return UserInformationOByGiteeVo 用户信息
     */
    @Override
    public Res<UserInformationByGiteeVo> parseLoginSigerByGitee(String code) throws IOException {
        //1. 根据传进来的code获取gitee的token
        //1.1 拼接参数
        LinkedHashMap<String, String> getTokenParams = new LinkedHashMap<>();
        getTokenParams.put(GET_ACCESS_TOKEN.getParams()[0], giteeProperty.getClientId());
        getTokenParams.put(GET_ACCESS_TOKEN.getParams()[1], giteeProperty.getClientSecret());
        getTokenParams.put(GET_ACCESS_TOKEN.getParams()[2], code);
        getTokenParams.put(GET_ACCESS_TOKEN.getParams()[3], giteeProperty.getUri().getLoginRedirectUri());
        //1.2 发送请求
        String getAccessTokenResponseBodyStr = HttpTool.sendPost(GiteeGetRequestPathAndParamsConstant.GET_ACCESS_TOKEN.getPath(), getTokenParams, null, null);
        //1.3 转换为对象
        log.info("getAccessTokenResponseBody:{}", getAccessTokenResponseBodyStr);
        GiteeTokenVo giteeTokenVo = JSONObject.parseObject(getAccessTokenResponseBodyStr, GiteeTokenVo.class);
        //2. 根据token获取用户信息
        //2.1 拼接参数
        LinkedHashMap<String, String> getUserInformationParams = new LinkedHashMap<>();
        if (giteeTokenVo != null) {
            getUserInformationParams.put(GiteeGetRequestPathAndParamsConstant.GET_USER_INFORMATION.getParams()[0], giteeTokenVo.getAccessToken());
        }
        //2.2 发送请求
        String responseBody = HttpTool.sendGet(GiteeGetRequestPathAndParamsConstant.GET_USER_INFORMATION.getPath(), getUserInformationParams, null, null);
        //2.3 解析用户信息
        if (responseBody != null) {
            UserInformationByGiteeVo userInformationByGiteeVo = JSONObject.parseObject(responseBody, UserInformationByGiteeVo.class);
            return new Res<>(GET_GITEE_USER_INFO_SUCCESS, userInformationByGiteeVo);
        } else {
            return Res.error();
        }
    }
}
